package com.adingxiong.quickstart.project.controller.util;

import com.adingxiong.quickstart.common.bean.MailBean;
import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.adingxiong.quickstart.util.MailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @ClassName EamilController
 * @Description TODO
 * @Author xiongchao
 * @Date 2020/5/27 17:35
 **/
@Api(tags = "邮箱接口")
@RequestMapping("/email")
@RestController
public class EamilController {

    @Autowired
    private MailUtils mailUtils;


    @ApiOperation(value = "邮箱验证码(注册和密码找回)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sendName", value = "收件人姓名", required = true, paramType = "query", example = "熊超"),
            @ApiImplicitParam(name = "sendEm", value = "收件人邮箱", required = true, paramType = "query", example = "1374543195@qq.com")
    })
    @RequestMapping(value = "/sendEmail", method = RequestMethod.GET)
    public Result sendEmail(String sendName, String sendEm) {
        if (StringUtils.isEmpty(sendEm) || StringUtils.isEmpty(sendEm)) {
            return new Result().setCode(RetCode.PARAM_ERROR.getCode()).setMsg("参数为空");
        }
        MailBean mailBean = new MailBean();
        mailBean.setRecipient(sendEm);
        String msgcode = String.valueOf(new Random().nextInt(899999) + 100000);
        String content = "<h2>尊敬的" + sendName + "：</h2>" +
                "<p style='padding-left:50px;'>【注册验证码】: 验证码为:<span style='font-size:16px;font-weight:bold'>" + msgcode + "</span>" +
                " （有效期为五分钟）</p>";
        mailBean.setContent(content);
        mailBean.setSubject("系统注册");
        try {
            mailUtils.sendHTMLMail(mailBean);
            return new Result().setCode(RetCode.SUCCESS).setMsg("邮箱发送成功");
        } catch (Exception e) {
            return new Result().setCode(RetCode.FAIL).setMsg("邮箱注册异常");
        }
    }
}
